<?php


namespace backend\controllers;


use common\helpers\commonApi;
use common\models\Order;
use common\models\System;
use Yii;


class SystemController extends BaseController
{
    private static $navAll = [
        ['name'=>'平台设置','url'=>'system/platform'],
        ['name'=>'微信设置','url'=>'system/wechat'],
        ['name'=>'存储设置','url'=>'system/storage'],
    ];
    public $Mmodel;
    public function init()
    {
        parent::init();
        $this->model = System::className();
        Yii::$app->view->params['nav_all'] = self::$navAll;
        $model = System::find()->orderBy(['id'=>SORT_DESC])->one();
        if (!$model){
            $model = new System();
        }
        $this->Mmodel = $model;
    }

    /**
     * 平台设置
     * @return string
     */
    public function actionPlatform()
    {
        return $this->render('',['model'=>$this->Mmodel]);
    }

    public function actionWechat()
    {
        $model = System::getModel();
        if (Yii::$app->request->isPost){
            $post = Yii::$app->request->post();
            $model->load($post,'');
            $model->cert_path = $post['filearr1'][0]??'';
            $model->key_path = $post['filearr2'][0]??'';
            if ($model->validate()&&$model->save()){
                return commonApi::message('设置成功');
            }
            $msg = commonApi::getModelError($model);
            return commonApi::message($msg,false);
        }
        return $this->render('',['model'=>$model]);
    }

    /**
     * 对象存储
     * @return array|string
     */
    public function actionStorage()
    {
        return $this->render('',['model'=>$this->Mmodel]);
    }

    /**
     * 保存
     * @return array
     */
    public function actionPreserve()
    {
        if (Yii::$app->request->isPost){
            $post = Yii::$app->request->post();
            $this->Mmodel->load($post,'');
            if ($this->Mmodel->validate()&&$this->Mmodel->save()){
                return commonApi::message('设置成功');
            }
            $msg = commonApi::getModelError($this->Mmodel);
            return commonApi::message($msg,false);
        }
    }
}